<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :append-to-body="true"
      @close="handleClose"
    >
      <el-form label-width="80px" :model="formDate" :rules="rules" ref="ruleForm">
        <el-form-item label="阶段名称:" prop="stepName">
          <el-input
            type="text"
            placeholder="请输入阶段名称"
            v-model="formDate.stepName"
            size="small"
            clearable
            style="width: 60%"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="开发周期:" prop="stepDays">
            <el-input
              type="text"
              placeholder="请输入开发周期"
              v-model="formDate.stepDays"
              size="small"
              clearable
              style="width: 60%"
            >
            </el-input>
            <span>(天)</span>
        </el-form-item>
        <el-form-item label="排序:">
          <el-input-number
          v-model="formDate.sortOrder"
          :min="0"
          label="请排序">
          </el-input-number>
        </el-form-item>
        <el-form-item label="状态:">
          <el-radio-group v-model="formDate.isEnable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注:">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入备注"
            v-model="formDate.stepRemark"
            clearable
            style="width: 60%"
            maxlength="200"
            show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { editDevelopStepAPI, getDevelopStepDetailAPI } from "@/api/fms.js";
const defaultListQuery = {
  stepName: "", //阶段名称
  stepDays: "", //开发周期
  sortOrder: 0,//排序值
  isEnable: 0,//是否启用
  stepRemark: "",//备注
};
export default {
  name: "addDevelopStage",
  data() {
    return {
      dialogVisible: false,
      formDate: JSON.parse(JSON.stringify(defaultListQuery)),
      title:"",
      rules: {
        stepName: [
          { required: true, message: '请输入阶段名称', trigger: 'blur' },
        ],
        stepDays: [
          { required: true, message: '请输入开发周期', trigger: 'blur' },
        ],
      }
    };
  },
  created() {
  },

  methods: {
    // 提交
    async btnOk() {
      let res = null
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.formDate.stepId) {
            res = await editDevelopStepAPI(this.formDate);
          } else {
            res = await editDevelopStepAPI(this.formDate);
          }
          if (res) {
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          }
          this.handleClose();
          this.$emit("upload");
        } else {
          return false;
        }
       })
    },
    // 回显
    async showDetail(stepId) {
      this.dialogVisible = true;
      this.title = stepId ? "修改" : "新增"
      if (stepId) {
        const res = await getDevelopStepDetailAPI({
          stepId: stepId
        })
        this.formDate={...res}
      }
    },

    // 关闭弹框
    handleClose() {
      this.formDate = JSON.parse(JSON.stringify(defaultListQuery))
      this.$refs.ruleForm.resetFields();
      this.dialogVisible = false;
    },
  },
};
</script>
<style lang="scss">
.avatar-uploader-icon {
  margin-bottom: 10px;
  display: inline-block;
  width: 80px;
  height: 80px;
  border: 1px solid #dcdfe6;
  text-align: center;
  line-height: 80px;
  vertical-align: middle;
}
</style>
